const {
  getWalletInfo,
  getRechargeList,
  getCashList,
  getIncomeList
} = require('../../../api/user-center-api')
// pages/user-center/wallet/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showRecharge: true,
    showGetcash: true,
    walletInfo: {},
    rechargeList: [], //充值
    rechargePage: 1,
    rechargePageSize: 10,
    rechargeTotalPage: 0,
    cashList: [], //提现
    cashPage: 1,
    cashPageSize: 10,
    cashTotalPage: 0,
    incomeList: [], //收入
    incomePage: 1,
    incomePageSize: 10,
    incomeTotalPage: 0,
  },
  // 充值提现事件
  onTabEvent(e) {
    let type = e.target.dataset.type;
    if (type == 1) {
      this.setData({
        showRecharge: true
      })
    } else {
      this.setData({
        showRecharge: false
      })
      // 点击提现获取提现明细
      let params = {
        page: this.data.cashPage,
        page_size: this.data.cashPageSize
      }
      this.getCashListInfo(params)
    }
  },
  /**
   * 获取提现明细
   */
  getCashListInfo(params) {
    let _this = this;
    getCashList(params).then(res => {
      let newListData = []
      if (_this.data.cashList.length > 0) {
        newListData = _this.data.cashList.concat(res.list)
      } else {
        newListData = res.list
      }
      _this.setData({
        cashList: newListData, //提现
        cashPage: res.page,
        cashPageSize: res.page_size,
        cashTotalPage: res.total_page,
      })

    })
  },
  /**
   * tab 收入提现事件
   */
  onGetCashEvent(e) {
    let type = e.target.dataset.type;
    if (type == 1) {
      this.setData({
        showGetcash: true
      })
    } else {
      this.setData({
        showGetcash: false
      })
      console.log('走这儿')
      // 收入明细
      let params = {
        page: this.data.incomePage,
        page_size: this.data.incomePageSize
      }
      this.getIncomeListInfo(params)
    }
  },
  /**
   * 获取收入明细列表
   */
  getIncomeListInfo (params) {
    let _this = this;
    getIncomeList(params).then(res => {
      let newListData = []
      if (_this.data.incomeList.length > 0) {
        newListData = _this.data.incomeList.concat(res.list)
      } else {
        newListData = res.list
      }
      _this.setData({
        incomeList: newListData,
        incomePage: res.page,
        incomePageSize: res.page_size,
        incomeTotalPage: res.total_page,
      })
    })
  },
  /**
   * 底部充值弹出框事件
   */
  onShowRechargeModal() {
    let rechargeModal = this.selectComponent('#rechargeModal')
    rechargeModal.showModal()
  },
  /**
   * 更新元宝数量 
   */
  updateYBNum(param) {
    let yuanbao = Number(param.detail),
      curYbNum = Number(this.data.walletInfo.yuanbao),
      updateYB = curYbNum + yuanbao,
      updateYBKey = 'walletInfo.yuanbao';
    this.setData({
      [updateYBKey]: updateYB
    })
    console.log('更新后的元宝数', updateYB)
  },

  /**
   * 底部提现弹出框事件
   */
  onShowGetCashModal() {
    let getCash = this.selectComponent('#getCash')
    getCash.showModal()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this;
    getWalletInfo().then(res => {
      _this.setData({
        walletInfo: res
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  /**
   * 获取充值明细
   */
  getRechargeListInfo(params) {
    let _this = this;
    getRechargeList(params).then(res => {
      let newListData = []
      if (_this.data.rechargeList.length > 0) {
        newListData = _this.data.rechargeList.concat(res.list)
      } else {
        newListData = res.list
      }
      _this.setData({
        rechargeList: newListData,
        rechargePage: res.page,
        rechargePageSize: res.page_size,
        rechargeTotalPage: res.total_page,
      })
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let _this = this;
    let rechargeParams = {
      page: _this.data.rechargePage,
      page_size: _this.data.rechargePageSize
    }
    _this.getRechargeListInfo(rechargeParams)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    // 充值明细下拉
    if (this.data.rechargePage < this.data.rechargeTotalPage) {
      let rechargeParams = {
        page: this.data.rechargePage + 1,
        page_size: this.data.rechargePageSize
      }
      this.getRechargeListInfo(rechargeParams)
    } else {
      wx.showToast({
        title: '我是有底线的',
        icon: 'none',
        duration: 1500
      })
    }

    // 提现明细下拉更新
    if (this.data.cashPage < this.data.cashTotalPage) {
      let getCashParams = {
        page: this.data.cashPage + 1,
        page_size: this.data.cashPageSize
      }
      this.getRechargeListInfo(getCashParams)
    } else {
      wx.showToast({
        title: '我是有底线的',
        icon: 'none',
        duration: 1500
      })
    }
     // 收入明细下拉更新
     if (this.data.incomePage < this.data.cashTotalPage) {
      let incomeParams = {
        page: this.data.incomePage + 1,
        page_size: this.data.incomePageSize
      }
      this.getIncomeListInfo(incomeParams)
    } else {
      wx.showToast({
        title: '我是有底线的',
        icon: 'none',
        duration: 1500
      })
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})